package com.jsj.isdt.servlet;

import com.alibaba.fastjson.JSON;
import com.jsj.isdt.entity.Book;
import com.jsj.isdt.entity.Car;
import com.jsj.isdt.utils.DBUtils;
import com.jsj.isdt.utils.ResultData;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.List;

public class ServletCar extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action") == null ? "" : req.getParameter("action");
        switch (action) {

            case "getone":
                getOne(req, resp);
                break;
            case "addcar":
                addCar(req, resp);
                break;
            case "getdglist":
                getDglist(req, resp);
                break;
            case "delete":
                Delete(req, resp);
                break;
            case "delete1":
                Delete1(req, resp);
                break;
            default:
                break;

        }


    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req,resp);
    }
    private  void  getOne(HttpServletRequest req, HttpServletResponse resp){

        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        try {
            PrintWriter out = resp.getWriter();
            String bname = req.getParameter("bname");
            String sql = "select bname,bprice from book where bname= ?";
            Book book =   DBUtils.QueryBean(sql, Book.class,bname);
            ResultData rd= new ResultData();
            if ( book != null){
                rd.setCode(String.valueOf(resp.getStatus()));
                rd.setMsg("获取成功");
                rd.setData( book);
                out.write(JSON.toJSONString(rd));
            }
            else {
                rd.setCode("S01");
                rd.setMsg("获取失败");
                out.write(JSON.toJSONString(rd));
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void  addCar(HttpServletRequest req, HttpServletResponse resp)throws IOException {
        String bname =  new String(req.getParameter("bname").getBytes("iso-8859-1"),"utf-8");
        String bprice = req.getParameter("bprice");
        String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF8&useSSL=true&serverTimezone=UTC";
        String dbusername = "root";
        String dbpassword = "123456";
        //1.加载驱动
        try {PrintWriter out = resp.getWriter();
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url, dbusername, dbpassword);
            String sql ="insert into car(bname,bprice) values (?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,bname);
            ps.setString(2,bprice);
            ResultData rd =new ResultData();
            if (ps.executeUpdate() >0){
                rd.setCode("200");
                rd.setMsg("成功");
            }
            else {  rd.setCode("501");
                rd.setMsg("失败");
            }
            out.write(JSON.toJSONString(rd));
        } catch (Exception e) {

            System.out.println(e.getMessage());
        }
    }
    private void getDglist(HttpServletRequest req, HttpServletResponse resp)  {
        try {
            PrintWriter out = resp.getWriter();
            String sql = "select * from car";
            List<Car> list = DBUtils.testDemoQueryBeanList(sql, Car.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", list.size());
            map.put("rows", list);
            out.write(JSON.toJSONString(map));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }
    private  void  Delete(HttpServletRequest req, HttpServletResponse resp){
        try {
            PrintWriter out = resp.getWriter();
            String bname = req.getParameter("bname");
            String sql = "delete from car where bname =?";
            if (DBUtils.testDemoUpdate(sql,bname) >0){
                out.write(JSON.toJSONString(new ResultData("200","删除成功")));
            }
            else
                out.write(JSON.toJSONString(new ResultData("500","删除失败")));

        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private  void  Delete1(HttpServletRequest req, HttpServletResponse resp){
        try {
            PrintWriter out = resp.getWriter();
            String id = req.getParameter("id");
            String sql = "delete from bookorder where id =?";
            if (DBUtils.testDemoUpdate(sql,id) >0){
                out.write(JSON.toJSONString(new ResultData("200","删除成功")));
            }
            else
                out.write(JSON.toJSONString(new ResultData("500","删除失败")));

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

